package com.yuan.gather.model;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.Getter;
import lombok.Setter;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 采集项的次分类
 *
 * @author zhaoff
 */
@Getter
@Setter
@XmlAccessorType(XmlAccessType.FIELD)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class GatherXmlCategoryBean {

    /**
     * 次分类的名字
     */
    @XmlAttribute
    private String name;

    /**
     * 名字-用于界面显示
     */
    @XmlAttribute
    private String display;

    /**
     * 此次分类下的所有巡检项分组
     */
    @XmlElement(name = "group")
    private List<GatherGroup> group;

    /**
     * 类与对象映射对象
     */
    private static final ObjectMapper mapper = new ObjectMapper();

    /**
     * 返回所有监控项
     *
     * @return 所有监控项
     */
    public List<GatherDetail> listIndicators() {
        List<GatherDetail> list = new ArrayList<>();

        for (GatherGroup group : getGroup()) {
            list.addAll(group.listBaselines());
            if (group.getGroup() != null) {
                for (GatherGroup chdGro : group.getGroup()) {
                    chdGro.setParentGroup(group);
                    listIndicators(list, chdGro);
                }
            }
        }
        return list;
    }

    /**
     * 获取分组中的所有采集项
     *
     * @param list       存放监控项的list
     * @param childGroup 分组信息
     */
    public void listIndicators(List<GatherDetail> list, GatherGroup childGroup) {
        list.addAll(childGroup.listBaselines());
        if (childGroup.getGroup() != null) {
            for (GatherGroup chdGro : childGroup.getGroup()) {
                chdGro.setParentGroup(childGroup);
                listIndicators(list, chdGro);
            }
        }
    }

    /**
     * 将另一个采集项的次分类合并到此次分类中
     *
     * @param newCategory 待合并的采集项的次分类
     * @return 新的采集项的次分类
     */
    public GatherXmlCategoryBean merge(GatherXmlCategoryBean newCategory) {
        List<GatherGroup> originGroups = this.getGroup();
        List<GatherGroup> newGroups = newCategory.getGroup();
        if (originGroups == null) {
            this.setGroup(newGroups);
            return this;
        }
        if (newGroups == null) {
            return this;
        }
        for (GatherGroup newGroup : newGroups) {
            boolean found = false;
            for (int i = originGroups.size() - 1; i >= 0; i--) {
                GatherGroup originGroup = originGroups.get(i);
                if (newGroup.getName().equals(originGroup.getName())) {
                    originGroup.merge(newGroup);
                    found = true;
                    break;
                }
            }
            if (!found) {
                originGroups.add(newGroup);
            }
        }
        return this;
    }

    @Override
    public GatherXmlCategoryBean clone() throws CloneNotSupportedException {
        GatherXmlCategoryBean bean = null;
        List<GatherGroup> copyGroup;
        try {
            bean = (GatherXmlCategoryBean) super.clone();
            JavaType javaTypeGro = mapper.getTypeFactory().constructParametricType(List.class, GatherGroup.class);
            copyGroup = mapper.readValue(mapper.writeValueAsBytes(this.group), javaTypeGro);
            bean.setGroup(copyGroup);
        } catch (IOException e) {
            //log.warn(null, e);
        }
        return bean;
    }

}
